<?php

/**
 * @author chenshubo、huj、lsm
 * 行云供应商（KA/POP）模式
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal;
use App\Model\PssModel\OrderSupplier;
use Service\AppService\IdService;
use App\Service\OrderService\BaseSupplierRouterService;
use App\Service\VendorClient\ShopXingYun\ShopXingYunApiClient;

class Shopxingyun extends BaseSupplierRouterService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {

        //下单商品重组
        $supplierGoodsInfo  = $param['supplierGoodsInfo'];
        $userGoodsList      = $supplierGoodsInfo['userGoodsList'];
        $goodsList          = $supplierGoodsInfo['goodsList'];

        //根据defaultStock分组
        $supplierCommodityTypeGroupGoodsList = [];
        foreach ($goodsList as $item ) {
            if ($item['supplierCommodityType']== GOODS_TYPE_BS){ //等于1的情况 保税商品
                $supplierCommodityTypeGroupGoodsList[GOODS_TYPE_BS][] = $item;
            }else{  //等于0和2的情况 一般商品和直邮商品
                $supplierCommodityTypeGroupGoodsList[GOODS_TYPE_YBMY][] = $item;
            }
            unset($item);
        }

        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;

        //按照上游仓库进行拆单
        foreach ($supplierCommodityTypeGroupGoodsList as $item )
        {
            $currentGoods = current($item);
            $supplierCommodityType = $currentGoods['supplierCommodityType'];

            //重新合并每一个仓库商品
            $orderGoods = [];
            foreach ($item as $goods ) {
                $orderGoods = array_merge($orderGoods,$userGoodsList[$goods['skuCode']]);
                unset($goods);
            }

            //生成供应商主单号
            $supplierCode = $param['supplierGoodsInfo']['supplierCode'];
            $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
            $param['order']['supplier_csn'] = IdService::instance()->getOtherID();

            if( $isDelare && !$isShop && $supplierCommodityType != GOODS_TYPE_YBMY ) {
                $param['order']['supplier_dsn'] = $param['order']['orderSn'];
            } else {
                $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
            }

            $param['order']['goods'] = $orderGoods;
            $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
            if( $res['code'] != Normal::SUC ) {
                return $res;
            }
            $orderItems[] = $res['data'];
        }

        return Normal::returnCode(Normal::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }

    /**
     * 订单申报
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        //根据skucode 查询净重和毛重
        $supplierSkucodeList = array_column($param['orderGoods'], 'supplier_skucode');
        $priceInfoArr = $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->channelQueryPrice($supplierSkucodeList);
        foreach ($param['orderGoods'] as &$orderGood) {
            if (isset($priceInfoArr[$orderGood['supplier_skucode']])) {
                $priceInfo = $priceInfoArr[$orderGood['supplier_skucode']];
                if (!isset($priceInfo['daily_quote']['sku_quotes']) || empty($priceInfo['daily_quote']['sku_quotes'])) {
                    $orderGood['package_num'] = 0;
                }
                $quotesList = array_column($priceInfo['daily_quote']['sku_quotes'], null, 'start_num');
                if(!isset($quotesList[$orderGood['spec_num']])){
                    $orderGood['package_num'] = 0;
                }
                $orderGood['package_num'] = (int)($quotesList[$orderGood['spec_num']]['pakage_num']);
            }else{
                $orderGood['package_num'] = 0;
            }
            unset($orderGood);
        }
        return $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->createOrder($param);
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->cancelOrder($param);
    }

    /**
     * 获取物流信息
     * @param array $param
     */
    public function orderExpress( $param = [] )
    {
        return Normal::returnCode(Normal::SUC,"处理完成");
    }

    /**
     * 商品列表查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsList( $param = [] )
    {
        $res = $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total' => $res['data']['total'],
            'list'  => $res['data']['data']
        ]);
    }

    /**
     * 商品详情查询接口
     * @param array $param
     * @return mixed
     */
    public function queryGoodsDetail( $param = [] )
    {
        $res = $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'total' => $res['data']['total'],
            'list'  => $res['data']['data']
        ]);
    }
    
    /**
     * 获取商品所有分类
     * @param array $param
     * @return mixed
     */
    public function queryGoodsCategory( $param = [] )
    {
        return $this->callSdkApi(ShopXingYunApiClient::class,$param['apiBody'])->{__FUNCTION__}($param['queryBody']);
    }
}
